﻿using System;
using System.Collections.Generic;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using YZQ_DotNetArx.CAD;
using Autodesk.AutoCAD.Colors;

namespace YZQ_DotNetArx.Test
{
    public enum HatchGradientName
    {
        /// <summary>
        /// 线状渐变
        /// </summary>
        Linear,
        /// <summary>
        /// 圆柱状渐变
        /// </summary>
        Cylinder,
        /// <summary>
        /// 反圆柱状渐变
        /// </summary>
        Invcylinder,
        /// <summary>
        /// 球状渐变
        /// </summary>
        Spherical,
        /// <summary>
        /// 反球状渐变
        /// </summary>
        Invspherical,
        /// <summary>
        /// 半球状渐变
        /// </summary>
        Hemisperical,
        /// <summary>
        /// 反半球状渐变
        /// </summary>
        InvHemisperical,
        /// <summary>
        /// 抛物面状渐变
        /// </summary>
        Curved,
        /// <summary>
        /// 反抛物面状渐变
        /// </summary>
        Incurved
    }
    public static class PLTC
    {
        [CommandMethod("PLTC")]
        public static void PLTCYS()
        {
            PromptSelectionResult psr = Env.Editor.SelectAll();
            if (psr.Status != PromptStatus.OK) return;
            using (Transaction trans = Env.Database.TransactionManager.StartTransaction())
            {
                foreach (ObjectId id in psr.Value.GetObjectIds())
                {
                    Polyline pl = trans.GetObject(id, OpenMode.ForRead) as Polyline;
                    LayerTableRecord ltr = (LayerTableRecord)pl.LayerId.GetObject(OpenMode.ForRead);
                    //创建一个ObjectId集合类对象，用于存储填充边界的ObjectId
                    ObjectIdCollection ids = new ObjectIdCollection();
                    ids.Add(id);
                    Hatch hatch = new Hatch();//创建填充对象
                    //创建两个Color类变量，分别表示填充的起始颜色（红）和结束颜色（蓝）
                    Color color1 = ltr.Color;
                    Color color2 = ltr.Color;
                    //创建渐变填充，与边界无关联
                    hatch.CreateGradientHatch(HatchGradientName.Cylinder, color1, color2, true);
                    //为填充添加边界（三角形）
                    hatch.LayerId=pl.LayerId;
                    hatch.AppendLoop(HatchLoopTypes.Default, ids);
                    hatch.EvaluateHatch(true);//计算并显示填充对象                 
                }
                trans.Commit();//提交更改
            }
        }
        /// <summary>
        /// 创建渐变色填充
        /// </summary>
        /// <param name="hatch">填充对象</param>
        /// <param name="gradientName">渐变色填充的图案名称</param>
        /// <param name="color1">渐变色填充的起始颜色</param>
        /// <param name="color2">渐变色填充的结束颜色</param>
        /// <param name="associative">填充是否与边界关联</param>
        public static void CreateGradientHatch(this Hatch hatch, HatchGradientName gradientName, Color color1, Color color2, bool associative)
        {
            //设置渐变色填充的类型所代表的字符串，用于将HatchGradientName枚举转换为对应的字符串
            string[] gradientNames = new string[] { "Linear","Cylinder","Invcylinder","Spherical",
                                            "Invspherical","Hemisperical","InvHemisperical","Curved","Incurved"};
            Database db = HostApplicationServices.WorkingDatabase;
            hatch.SetDatabaseDefaults();//设置填充的默认值
            //设置填充的类型为渐变色填充
            hatch.HatchObjectType = HatchObjectType.GradientObject;
            //设置渐变色填充的类型和图案名称
            hatch.SetGradient(GradientPatternType.PreDefinedGradient, gradientNames[(int)gradientName]);
            //新建两个Color类对象，分别表示渐变色填充的起始和结束颜色
            GradientColor gColor1 = new GradientColor(color1, 0);
            GradientColor gColor2 = new GradientColor(color2, 1);
            //设置渐变色填充的起始和结束颜色
            hatch.SetGradientColors(new GradientColor[] { gColor1, gColor2 });
            //db.AddToModelSpace(hatch);//将填充添加到模型空间
            Env.AddEntityToModelSpace(hatch);
            //设置填充与边界是否关联
            hatch.Associative = associative ? true : false;
        }
    }
}
